﻿using UnityEngine;
using System.Collections;
using Dict.DataModel;

namespace Room 
{
    public class RCLODRaidHold : RCLODBase 
    {
        public RCLODRaidHold(RoomCharactor owner) : base(owner) {
            type = LODType.Stronghold;
        }

        public StrongholdState currentState;
        Dict.DataModel.TableWorldBossStronghold dictData;
        Dict.DataModel.TableWorldBossStrongholdStyle style;
        public int rewardTimes;
        public System.DateTime nextRefreshTime;

        bool running = false;
        bool inView = false;

        bool weaponLoaded = false;
        string leftWeaponResName = "";
        string rightWeaponResName = "";

        GameObject fightParticle = null;
        GameObject defeatParticle = null;
        GameObject rewardParticle = null;

        protected string modelResName = "";
        protected float modelScale = 1f;
        protected float modelHeight = 0f;

        public void Init(ProtoBuf.Message.WorldBossStrongholdInfo info) {
            dictData = Dict.Blo.DictWorldBossBlo.GetTableWorldBossStronghold(info.strongholdId);
            style = Dict.Blo.DictWorldBossBlo.GetTableWorldBossStrongholdStyle(dictData.StrongholdStyleId);

            if (style != null) {
                modelResName = style.ModelName;
                modelScale = (float)style.ModelScale;
                modelHeight = 1f;       // TEMP
                leftWeaponResName = style.LeftWeaponName;
                rightWeaponResName = style.RightWeaponName;
            }

            SetData(info);
        }

        public void SetData(ProtoBuf.Message.WorldBossStrongholdInfo info) {
            rewardTimes = info.rewardTimes;
            nextRefreshTime = ServerTimerTool.Java2CSTimeLocale(info.nextRefreshTime);
            currentState = (StrongholdState)info.state;
        }


        public override void DoResume()
        {
            base.DoResume();
            running = true;
            Refresh();
        }

        public override void DoPause()
        {
            base.DoPause();
            running = false;
            Refresh();
        }

        public override void DoStop()
        {
            UnloadResource();
        }

        public override void SetInView(bool inView)
        {
            this.inView = inView;
            Refresh();
        }

        public override float GetHeight()
        {
            return modelHeight;
        }

        public override void SetLevel(int lodLevel)
        {
            base.SetLevel(lodLevel);
            Refresh();
        }


        void ShowFightParticle() {
            if (fightParticle != null) {
                fightParticle.transform.SetParent(owner.transform);
                fightParticle.transform.localEulerAngles = Vector3.zero;
                fightParticle.transform.localPosition = new Vector3(0, GetHeight() + 0.5f, 0);
                fightParticle.transform.localScale = Vector3.one;
                fightParticle.SetActive(true);
            }
        }

        void HideFightParticle() {
            if (fightParticle != null) {
                fightParticle.SetActive(false);
            }
        }

        void ShowRewardParticle() {
            if (rewardParticle != null) {
                rewardParticle.transform.SetParent(owner.transform);
                rewardParticle.transform.localEulerAngles = Vector3.zero;
                rewardParticle.transform.localPosition = Vector3.zero;
                rewardParticle.transform.localScale = Vector3.one;
                rewardParticle.transform.SetParent(owner.transform);
                rewardParticle.SetActive(true);
            }

        }

        void HideRewardParticle() {
            if (rewardParticle != null) {
                rewardParticle.SetActive(false);
            }
        }

        void ShowDefeatParticle() {
            if (defeatParticle != null)
            {
                defeatParticle.transform.SetParent(owner.transform);
                defeatParticle.transform.localEulerAngles = Vector3.zero;
                defeatParticle.transform.localPosition = Vector3.zero;
                defeatParticle.transform.localScale = Vector3.one;
                defeatParticle.SetActive(true);
            }

        }

        void HideDefeatParticle() {
            if (defeatParticle != null) {
                defeatParticle.SetActive(false);
            }
        }

        public void Refresh() {
            RefreshResource();
            if (running)
            {
                switch (currentState)
                {
                    case StrongholdState.Defend:
                        if (model != null && model.GetComponent<LegacyAnimation>() != null)
                        {
                            model.SetActive(true);
                            model.GetComponent<LegacyAnimation>().Play(AniName.IDLE_FIGHT, WrapMode.Loop);
                        }
                        HideFightParticle();
                        HideRewardParticle();
                        HideDefeatParticle();
                        break;
                    case StrongholdState.Fighting:
                        if (model != null && model.GetComponent<LegacyAnimation>() != null)
                        {
                            model.SetActive(true);
                            model.GetComponent<LegacyAnimation>().Play(AniName.IDLE_FIGHT, WrapMode.Loop);
                        }
                        ShowFightParticle();
                        HideRewardParticle();
                        HideDefeatParticle();
                        break;
                    case StrongholdState.Rewarding:
                        if (model != null)
                        {
                            model.SetActive(false);
                        }
                        HideFightParticle();
                        ShowRewardParticle();
                        HideDefeatParticle();
                        break;
                    case StrongholdState.Defeat:
                        if (model != null)
                        {
                            model.SetActive(false);
                        }
                        HideFightParticle();
                        HideRewardParticle();
                        ShowDefeatParticle();
                        break;
                }
            }
            else {
                if (model != null)
                {
                    model.SetActive(false);
                }
                HideFightParticle();
                HideRewardParticle();
                HideDefeatParticle();
            }
        }

        protected override void OnLoadModelComplete()
        {
            model.GetComponent<ModelRenderKit>().SetModelScale(modelScale);
            CommonMethod.ChangeLayer(model.transform, LayerName.SceneActor);
            RefreshResource();
        }

        void UnloadResource() {
            UnloadWeapon();
            UnloadModel();
        }

        void RefreshResource() {
            if (running && level > 0 && inView)
            {
                if (model == null)
                {
                    LoadModel(style.ModelName);
                    return;
                }
                model.SetActive(true);
                ResumeAnim();

                if (!weaponLoaded)
                {
                    LoadWeapon();
                }

                if (fightParticle == null) {
                    fightParticle = GameObjectPoolManager.Instance.Spawn(RoomRaid.fightstateparticlepath, CommonMethod.GetAssetNameFromAssetBundleName(RoomRaid.fightstateparticlepath));
                    HideFightParticle();
                }
                if (rewardParticle == null) {
                    rewardParticle = GameObjectPoolManager.Instance.Spawn(RoomRaid.rewardstateparticlepath, CommonMethod.GetAssetNameFromAssetBundleName(RoomRaid.rewardstateparticlepath));
                    HideRewardParticle();
                }
                if (defeatParticle == null) {
                    defeatParticle = GameObjectPoolManager.Instance.Spawn(RoomRaid.defeatstateparticlepath, CommonMethod.GetAssetNameFromAssetBundleName(RoomRaid.defeatstateparticlepath));
                    HideDefeatParticle();
                }
            }
            else
            {
                if (model != null)
                {
                    model.SetActive(false);
                }
            }
        }


        protected virtual void LoadWeapon()
        {
            if (model != null)
            {
                owner.StartCoroutine(model.GetComponent<ModelRenderKit>().LoadWeaponsDirectly(leftWeaponResName, rightWeaponResName));
            }
        }

        protected virtual void UnloadWeapon()
        {
            if (model != null)
            {
                model.GetComponent<ModelRenderKit>().UnloadWeapons();
            }
        }


    }
}
